package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/dormitoryManagementSystem/model"
	"strconv"
)

type DormitoryController struct {
	Ctx iris.Context
	CheckInInformation *model.CheckInInformation
	ExchangeHouses *model.ExchangeHouses
}

func (dc *DormitoryController)PostCalculaterent() mvc.Result {

	dc.CheckInInformation = new(model.CheckInInformation)

	res, err := dc.CheckInInformation.CalculateRent()
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DormitoryController)PostCalculaterentasync() mvc.Result {

	dc.CheckInInformation = new(model.CheckInInformation)

	res, err := dc.CheckInInformation.CalculateRentAsync()
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DormitoryController)PostQuerycostbuilding() mvc.Result {

	dc.CheckInInformation = new(model.CheckInInformation)

	monthlyBuildingCostIdStr := dc.Ctx.FormValue("monthlyBuildingCostId")
	monthlyBuildingCostId, err := strconv.ParseInt(monthlyBuildingCostIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	buildingIdStr := dc.Ctx.FormValue("buildingId")
	buildingId, err := strconv.ParseInt(buildingIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	yearMonth := dc.Ctx.FormValue("yearMonth")

	res, err := dc.CheckInInformation.QueryCostBuilding(monthlyBuildingCostId, buildingId, yearMonth)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (dc *DormitoryController)PostBuildingstatistics() mvc.Result {

	dc.CheckInInformation = new(model.CheckInInformation)

	res, err := dc.CheckInInformation.BuildingStatistics()
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (dc *DormitoryController)PostExchangelock() mvc.Result {

	dc.ExchangeHouses = new(model.ExchangeHouses)

	checkInInformationIdStr := dc.Ctx.FormValue("checkInInformationId")
	checkInInformationId, err := strconv.ParseInt(checkInInformationIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	checkInIdStr := dc.Ctx.FormValue("checkInId")
	checkInId, err := strconv.ParseInt(checkInIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	buildingIdStr := dc.Ctx.FormValue("buildingId")
	buildingId, err := strconv.ParseInt(buildingIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.ExchangeHouses.ExchangeLock(checkInInformationId, checkInId, buildingId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}


































